import 'package:flutter/material.dart';
import 'package:study_flutter/yzhg/dartTools/ToastTools.dart';
import 'package:study_flutter/yzhg/dartTools/YSPTools.dart';

class SpDemoPage extends StatelessWidget {
  TextEditingController controller1 = TextEditingController();
  TextEditingController controller2 = TextEditingController();

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("SP保存数据"),
      ),
      body: Column(
        children: <Widget>[
          _inputView(controller1, Icon(Icons.vpn_key, size: 16.0),
              TextInputType.text, "输入SPKey"),
          _inputView(controller2, Icon(Icons.person, size: 16.0),
              TextInputType.text, "输入储存的数据"),
          _bottonView(() {
            var key = controller1.text;
            var value = controller2.text;
            if (key.isEmpty || value.isEmpty) {
              ToastTools.showToast("请输入key和value");
            } else {
              YSPTools.getInstance().putString(key, value);
              ToastTools.showToast("保存成功");
            }
          }, "点击保存"),
          _bottonView(() {
            var key = controller1.text;
            if (key.isEmpty) {
              ToastTools.showToast("请输入key");
            } else {
              YSPTools.getInstance().getString(key).then((value) {
                ToastTools.showToast("获取字符串${value.toString()}");
              });
            }
          }, "获取保存的字符串")
        ],
      ),
    );
  }


  Widget _inputView(TextEditingController controller, Icon icons,
      TextInputType textInputType, String hintText) {
    return Padding(
      padding: EdgeInsets.all(20.0),
      child: DecoratedBox(
        decoration: BoxDecoration(
            //设置背景为白色
            color: Colors.white,
            //设置边框
            border: Border.all(
                color: Colors.yellow, style: BorderStyle.solid, width: 1.0),
            //设置边框圆角
            borderRadius: BorderRadius.all(Radius.circular(30))),
        child: TextField(
          autofocus: false,
          //设置弹出数字键盘
          keyboardType: textInputType,
          controller: controller,
          textAlign: TextAlign.left,
          style: TextStyle(fontSize: 14.0),
          decoration: InputDecoration(
              //去除下划线
              border: InputBorder.none,
              //labelText:手机号
              hintText: hintText,
              prefixIcon: icons),
        ),
      ),
    );
  }

  Widget _bottonView(VoidCallback callback, String butMsg) {
    return Padding(
      padding: EdgeInsets.fromLTRB(40.0, 0.0, 40.0, 0.0),
      child: Container(
        width: double.infinity,
        child: FlatButton(
          onPressed: callback,
          color: Colors.blue,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(20.0),
          ),
          child: Text(
            butMsg,
            style: TextStyle(fontSize: 14.0, color: Colors.white),
          ),
        ),
      ),
    );
  }
}

